-- UIGuideLongPressMask
-- Created by zouyb
-- 长按出怪物信息窗口

UIGuideLongPressMask = class("UIGuideLongPressMask", function()
    return cc.Layer:create();
end);

function UIGuideLongPressMask.create(targetForm, path, callback)
    return UIGuideLongPressMask.new(targetForm, path, callback);
end

-- 构造函数
function UIGuideLongPressMask:ctor(targetForm, path, callback)
    self:setName("UIGuideLongPressMask");
    local node = cc.CSLoader:createNode("layout/guide/GuideLongPressMask.csb");
    self:addChild(node);
    self.node = node;
    self.path = path;
    self.callback = callback;
    
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    
    -- 获得目标节点
    local targetNode = findChildByName(targetForm.node, path);

    -- 其他部分变暗
    turnFormDark(targetForm, true, { path });

    if targetNode then
        self:init(targetNode);
    end    

    -- 点击回调处理
    EventMgr.register("UIGuideLongPressMask", event.TARGET_CLICK, function(param)
        if param.object == targetNode then   
            UIMgr.getCurrentScene():removeFormByName("UIGuideLongPressMask");
        end           
    end);    

    self:registerScriptHandler(function(event)
        if event == "exit" then
            turnFormDark(targetForm, false, { });
            EventMgr.removeAll("UIGuideLongPressMask");                  
        end
    end);

    self:resize();
end

-- 适配
function UIGuideLongPressMask:resize()
    local node = self.node;
	
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;
    
    local panel = findChildByName(self.node, "panel");
    panel:setScaleX(w / DESIGN_WIDTH);
    panel:setScaleY(h / DESIGN_HEIGHT);
    panel:setPosition(w / 2, h / 2);  
end

-- 初始化
function UIGuideLongPressMask:init(targetNode)
    local worldPos = targetNode:convertToWorldSpace(cc.p(0, 0));

    local panel = findChildByName(self.node, "panel");
    panel:setTouchEnabled(true);
    local nodePos = panel:convertToNodeSpace(worldPos);

    local rectNode = findChildByName(self.node, "panel/rect");
--    if DEBUG_MODE == 1 then
--        -- 测试模式时，增加点击范围区域显示
--        rectNode:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid);
--        rectNode:setBackGroundColor(cc.c3b(0, 128, 0));
--    end

    rectNode:setPosition(nodePos);
    rectNode:setScaleX(targetNode:getScaleX());
    rectNode:setScaleY(targetNode:getScaleY());  
    rectNode:setContentSize(targetNode:getContentSize().width, targetNode:getContentSize().height);
    
    local hand = rectNode:getChildByName("hand");
    hand:setVisible(true);
    hand:setPosition(rectNode:getContentSize().width / 2, rectNode:getContentSize().height / 2);
    playClickEffect(hand);
    
	local function longPressHandler(sender)
		local rect = rectNode:getBoundingBox();

		local locationInNode = panel:convertToNodeSpace(sender:getTouchBeganPosition());
		if cc.rectContainsPoint(rect, locationInNode) then
            if self.callback then
                self.callback();
            end
			return false;
		end

		-- 不在目标范围区域， 吃掉消息
		panel:setSwallowTouches(true);

		-- 播放涟漪效果          
		playEffect(panel, 1175, locationInNode.x, locationInNode.y);

		UIMgr.getCurrentScene():removeFormByName("UIGuideMask");
	end

    local panel = findChildByName(self.node, "panel");    
    local function onCloseClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
			performWithDelay(self, longPressHandler, LONG_PRESS_TIME);
		elseif eventType == ccui.TouchEventType.ended then
            self:stopAllActions();   

			alert(getLocStr("long_press_hint"));	
			-- 播放错误效果          
			-- playEffect(panel, 1175, locationInNode.x, locationInNode.y);
            return true;
        end
    end
    panel:addTouchEventListener(onCloseClick); 
end




